package com.grad.why.studentsystem.controller;

import com.grad.why.studentsystem.domain.Project;
import com.grad.why.studentsystem.domain.current.CurrentClassinfo;
import com.grad.why.studentsystem.domain.current.CurrentProject;
import com.grad.why.studentsystem.service.OptionparamsService;
import com.grad.why.studentsystem.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/studentManageSystem")
@Controller
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @Autowired
    private OptionparamsService optionparamsService;

    @RequestMapping("/getProjectInfos")
    @ResponseBody
    public Object getProjectInfos(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "order") String order
    ) {
        try {
            List<CurrentProject> rows = projectService.getProjectList(offset, limit, sort, search);
            long total = projectService.getProjectListLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/addProject")
    @ResponseBody
    public String addProject(Project project){
        try{
            project.setId(projectService.getMaxId() + 1);
            project.setStudentcount(0);
            int isExist = projectService.ProjectNameIsExist(project.getName());
            if(isExist > 0){
                return null;
            }else{
                int n  = projectService.addProjectByEntity(project);
                if(n > 0){
                    return "OK";
                }
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/uploadProjectInfos")
    @ResponseBody
    public String uploadProjectInfos(@RequestParam("file")MultipartFile file){
        try{
            String fileUploadRootPath = optionparamsService.getParamValueByParamName("fileRootPath");
            String fileName = file.getOriginalFilename();
            String uploadCurrentFileName = fileUploadRootPath+"uploadCameraInfo/"+ new Date().getTime() + fileName;

            File fileRootPath = new File(fileUploadRootPath+"uploadCameraInfo");

            if(!fileRootPath.exists()){
                fileRootPath.mkdirs();
            }

            FileOutputStream out = new FileOutputStream(uploadCurrentFileName);
            out.write(file.getBytes());
            out.flush();
            out.close();

            int n = projectService.addProjectInfosByExcelFile(uploadCurrentFileName);
            if(n<=0){
                return "添加失败";
            }else{
                return "添加"+n+"条数据";
            }
            //上传学生信息逻辑
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/deleteProjectinfo/{ids}")
    @ResponseBody
    public String deleteProjectinfo(@PathVariable("ids") String ids){
        try{
            String[] idArrays = ids.split("-");
            for(String id : idArrays){
                int n = projectService.deleteProjectinfo(Integer.parseInt(id));
                if(n<=0){
                    return null;
                }
            }
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
